from django.db import models
from users.models import User

class MerchantCategory(models.Model):
    name = models.CharField(max_length=50, verbose_name='分类名称')
    description = models.TextField(blank=True, verbose_name='分类描述')
    icon = models.ImageField(upload_to='category_icons/', blank=True, verbose_name='分类图标')
    
    class Meta:
        verbose_name = '商家分类'
        verbose_name_plural = verbose_name
        
    def __str__(self):
        return self.name

class Merchant(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name='用户')
    category = models.ForeignKey(MerchantCategory, on_delete=models.SET_NULL, null=True, verbose_name='商家分类')
    name = models.CharField(max_length=100, verbose_name='商家名称')
    description = models.TextField(blank=True, verbose_name='商家描述')
    address = models.CharField(max_length=200, verbose_name='商家地址')
    phone = models.CharField(max_length=11, verbose_name='联系电话')
    logo = models.ImageField(upload_to='merchant_logos/', blank=True, verbose_name='商家logo')
    is_active = models.BooleanField(default=True, verbose_name='是否营业')
    delivery_fee = models.DecimalField(max_digits=5, decimal_places=2, default=0, verbose_name='配送费')
    min_order_amount = models.DecimalField(max_digits=6, decimal_places=2, default=0, verbose_name='最低订单金额')
    business_hours = models.CharField(max_length=200, default='09:00-22:00', verbose_name='营业时间')
    delivery_radius = models.IntegerField(default=3000, verbose_name='配送范围(米)')
    rating = models.DecimalField(max_digits=2, decimal_places=1, default=5.0, verbose_name='评分')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        verbose_name = '商家'
        verbose_name_plural = verbose_name
        
    def __str__(self):
        return self.name

class MenuItem(models.Model):
    ITEM_CATEGORIES = [
        ('main', '主食'),
        ('side', '小菜'),
        ('drink', '饮品'),
        ('dessert', '甜点'),
    ]
    
    merchant = models.ForeignKey(Merchant, on_delete=models.CASCADE, related_name='menu_items', verbose_name='商家')
    category = models.CharField(max_length=20, choices=ITEM_CATEGORIES, default='main', verbose_name='菜品分类')
    name = models.CharField(max_length=100, verbose_name='菜品名称')
    description = models.TextField(blank=True, verbose_name='菜品描述')
    price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='价格')
    image = models.ImageField(upload_to='menu_items/', blank=True, verbose_name='菜品图片')
    is_available = models.BooleanField(default=True, verbose_name='是否可用')
    stock = models.IntegerField(default=999, verbose_name='库存数量')
    sales_count = models.IntegerField(default=0, verbose_name='销量')
    is_spicy = models.BooleanField(default=False, verbose_name='是否辣')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        verbose_name = '菜品'
        verbose_name_plural = verbose_name
        
    def __str__(self):
        return f"{self.merchant.name} - {self.name}"
